Skip to content

Commit

Permalink
Display European price when selling, IR#43, from Brian Kim, Louise Zh…
Browse files Browse the repository at this point in the history
…ang, Seongmin Park and Michael Jeffers
  • Loading branch information
Mike Pope committed Dec 20, 2016
1 parent 2bf00f6 commit 01abb56
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 16 deletions.
7 changes: 5 additions & 2 deletions data/strings/FreeColMessages.properties
Expand Up @@ -2753,7 +2753,7 @@ boycottedGoods.dumpGoods=Dump Goods
boycottedGoods.text=As %goods% have been boycotted by the Crown, you can not sell them in %europe%. Do you wish to pay your arrears (%amount% gold), or do you wish to dump the goods in the harbor, destroying them?
buy.moreGold=Ask for lower price
buy.takeOffer=Accept the offer
buy.text=The %nation% would like to sell their %goods% for %gold%:
buy.text=The %nation% would like to sell their %goods% for %gold%\n (European buy price: %euprice%)
clearTradeRoute.text=Your %unit% is assigned to trade route %route%. Setting its destination will drop it from the trade route. Are you sure you want to do this?
client.fullScreen=Full screen mode is not supported for this GraphicsDevice.\nFalling back to windowed mode.
confirmHostile.alliance=You can not attack an allied nation! Do you really wish to break your alliance with {{tag:country|%nation%}} and declare war?
Expand Down Expand Up @@ -2804,7 +2804,7 @@ scoutSettlement.tribute=Demand tribute
sell.gift=Offer the %goods% as a gift
sell.moreGold=Ask for more gold
sell.takeOffer=Accept the offer
sell.text=The %nation% would like to buy your %goods% for %gold%:
sell.text=The %nation% would like to buy your %goods% for %gold%\n (European sale price: %euprice%)
stopCurrentGame.no=Cancel
stopCurrentGame.text=A game is already running.
stopCurrentGame.yes=Stop Game
Expand Down Expand Up @@ -3304,6 +3304,7 @@ buildQueuePanel.units=Units

# CaptureGoodsDialog
captureGoodsDialog.title=Loot Cargo
captureGoodsDialog.europeanValue=European value: %gold%

# CargoPanel
cargoPanel.cargoAndSpace=Cargo on %name% (%space% {{plural:%space%|one=hold|other=holds|default=holds}} left)
Expand Down Expand Up @@ -3407,6 +3408,8 @@ negotiationDialog.title.contact=Meeting Fellow Europeans
negotiationDialog.title.diplomatic=Diplomatic Negotiation
negotiationDialog.title.trade=Trade Negotiation
negotiationDialog.title.tribute=Tribute Demand
negotiationDialog.euBuyPrice=(%priceTotal% in Europe)
negotiationDialog.euSalePrice=(%priceTotal% in Europe)

# DumpCargoDialog

Expand Down
10 changes: 8 additions & 2 deletions src/net/sf/freecol/client/gui/GUI.java
Expand Up @@ -707,10 +707,13 @@ public BoycottAction getBoycottChoice(Goods goods, Europe europe) {
*/
public TradeBuyAction getBuyChoice(Unit unit, Settlement settlement,
Goods goods, int gold, boolean canBuy) {
//Get Buy price on Europe Market for comparison
int euroPrice = unit.getOwner().getMarket().getBidPrice(goods.getType(), goods.getAmount());
StringTemplate template = StringTemplate.template("buy.text")
.addStringTemplate("%nation%", settlement.getOwner().getNationLabel())
.addStringTemplate("%goods%", goods.getLabel(true))
.addAmount("%gold%", gold);
.addAmount("%gold%", gold)
.addAmount("%euprice%", euroPrice);

List<ChoiceItem<TradeBuyAction>> choices = new ArrayList<>();
choices.add(new ChoiceItem<>(Messages.message("buy.takeOffer"),
Expand Down Expand Up @@ -950,11 +953,14 @@ public ScoutIndianSettlementAction getScoutIndianSettlementChoice(IndianSettleme
*/
public TradeSellAction getSellChoice(Unit unit, Settlement settlement,
Goods goods, int gold) {
//Get Sale price on Europe Market for comparison
int euroPrice = unit.getOwner().getMarket().getSalePrice(goods.getType(), goods.getAmount());
StringTemplate goodsTemplate = goods.getLabel(true);
StringTemplate template = StringTemplate.template("sell.text")
.addStringTemplate("%nation%", settlement.getOwner().getNationLabel())
.addStringTemplate("%goods%", goodsTemplate)
.addAmount("%gold%", gold);
.addAmount("%gold%", gold)
.addAmount("%euprice%", euroPrice);

List<ChoiceItem<TradeSellAction>> choices = new ArrayList<>();
choices.add(new ChoiceItem<>(Messages.message("sell.takeOffer"),
Expand Down
49 changes: 41 additions & 8 deletions src/net/sf/freecol/client/gui/dialog/CaptureGoodsDialog.java
Expand Up @@ -41,6 +41,8 @@
import net.sf.freecol.client.gui.panel.*;
import net.sf.freecol.common.i18n.Messages;
import net.sf.freecol.common.model.Goods;
import net.sf.freecol.common.model.Market;
import net.sf.freecol.common.model.StringTemplate;
import net.sf.freecol.common.model.Unit;


Expand Down Expand Up @@ -83,6 +85,20 @@ public Goods getGoods() {
}


public String pricePerGood(Market lookup) {
int total = 0;
if (lookup != null && goods != null) {
total = lookup.getBidPrice(goods.getType(), goods.getAmount());
}
StringTemplate template = StringTemplate
.template("captureGoodsDialog.europeanValue")
.addStringTemplate("%gold%", StringTemplate
.template("goldAmount")
.addAmount("%amount%", total));
return Messages.message(template);
}


// Override Object

/**
Expand All @@ -97,11 +113,23 @@ public String toString() {
private static class CheckBoxRenderer extends JCheckBox
implements ListCellRenderer<GoodsItem> {

private Market market;

public CheckBoxRenderer() {
//setBackground(UIManager.getColor("List.textBackground"));
//setForeground(UIManager.getColor("List.textForeground"));
}

/**
* Overload constructor for market lookups on good pricing for display
*
* @param forPriceLookup A {@code Market} to add extra price
* information from.
*/
public CheckBoxRenderer(Market forPriceLookup) {
this.market = forPriceLookup;
}

/**
* {@inheritDoc}
*/
Expand All @@ -111,8 +139,12 @@ public Component getListCellRendererComponent(JList<? extends GoodsItem> list,
int index,
boolean isSelected,
boolean hasFocus) {
if(market!=null){
setText(value.toString() +" "+ value.pricePerGood(market));
}else{
setText(value.toString());
}
setSelected(value.isSelected());
setText(value.toString());
setEnabled(value.isEnabled());
return this;
}
Expand Down Expand Up @@ -145,6 +177,13 @@ public CaptureGoodsDialog(FreeColClient freeColClient, JFrame frame,

this.maxCargo = winner.getSpaceLeft();

GoodsItem[] goods = new GoodsItem[loot.size()];
for (int i = 0; i < loot.size(); i++) {
goods[i] = new GoodsItem(loot.get(i));
}
this.goodsList = new JList<>();
this.goodsList.setListData(goods);

this.allButton = Utility.localizedButton("all");
this.allButton.addActionListener((ActionEvent ae) -> {
JList<GoodsItem> gl = CaptureGoodsDialog.this.goodsList;
Expand All @@ -170,13 +209,7 @@ public CaptureGoodsDialog(FreeColClient freeColClient, JFrame frame,
this.noneButton.setMnemonic('n');
this.noneButton.setActionCommand(this.noneButton.getText());

GoodsItem[] goods = new GoodsItem[loot.size()];
for (int i = 0; i < loot.size(); i++) {
goods[i] = new GoodsItem(loot.get(i));
}
this.goodsList = new JList<>();
this.goodsList.setListData(goods);
this.goodsList.setCellRenderer(new CheckBoxRenderer());
this.goodsList.setCellRenderer(new CheckBoxRenderer(winner.getOwner().getMarket()));
this.goodsList.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me) {
Expand Down
34 changes: 30 additions & 4 deletions src/net/sf/freecol/client/gui/dialog/NegotiationDialog.java
Expand Up @@ -64,6 +64,7 @@
import net.sf.freecol.common.model.GoodsLocation;
import net.sf.freecol.common.model.GoodsTradeItem;
import net.sf.freecol.common.model.InciteTradeItem;
import net.sf.freecol.common.model.Market;
import net.sf.freecol.common.model.NationSummary;
import net.sf.freecol.common.model.Ownable;
import net.sf.freecol.common.model.Player;
Expand Down Expand Up @@ -1118,11 +1119,36 @@ private void updateDialog() {
* Gets a trade item button for a given item.
*
* @param item The {@code TradeItem} to make a button for.
* @param saleDir Boolean to indicate the EU price for sale (T) or buy (F)
* @return A new {@code JButton} for the item.
*/
private JButton getTradeItemButton(TradeItem item) {
private JButton getTradeItemButton(TradeItem item, boolean saleDir) {

Market market = getMyPlayer().getMarket();
JButton button = new JButton(new RemoveAction(item));
button.setText(Messages.message(item.getLabel()));

// Checks if the items are goods
if (item.getGoods() != null) {
int buyPriceTotal = market.getBidPrice(item.getGoods().getType(), item.getGoods().getAmount());
int salePriceTotal = market.getSalePrice(item.getGoods().getType(), item.getGoods().getAmount());

// Depending on saleDir, creates a button for goods w/ EU buy or sale price
if (saleDir) {
button.setText(Messages.message(item.getLabel()) + " " +
Messages.message(StringTemplate
.template("negotiationDialog.euSalePrice")
.addAmount("%priceTotal%", salePriceTotal)));
} else {
button.setText(Messages.message(item.getLabel()) + " " +
Messages.message(StringTemplate
.template("negotiationDialog.euBuyPrice")
.addAmount("%priceTotal%", buyPriceTotal)));
}
} else {
// If not goods, follow protocol
button.setText(Messages.message(item.getLabel()));
}

button.setMargin(Utility.EMPTY_MARGIN);
button.setOpaque(false);
button.setForeground(Utility.LINK_COLOR);
Expand All @@ -1145,7 +1171,7 @@ private void updateSummary() {
if (!offers.isEmpty()) {
summary.add(Utility.localizedLabel(this.offer), "span");
for (TradeItem item : offers) {
summary.add(getTradeItemButton(item), "skip");
summary.add(getTradeItemButton(item, true), "skip");
}
}

Expand All @@ -1157,7 +1183,7 @@ private void updateSummary() {
summary.add(new JLabel(exchangeMessage), "newline 20, span");
}
for (TradeItem item : demands) {
summary.add(getTradeItemButton(item), "skip");
summary.add(getTradeItemButton(item, false), "skip");
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions test/src/net/sf/freecol/common/model/MarketTest.java
Expand Up @@ -19,6 +19,9 @@

package net.sf.freecol.common.model;

import java.util.ArrayList;
import java.util.List;

import net.sf.freecol.common.model.Specification;
import net.sf.freecol.util.test.FreeColTestCase;

Expand All @@ -38,12 +41,37 @@ public void testInitialMarket() {
Market dm = p.getMarket();

Specification s = spec();


for (GoodsType good : s.getStorableGoodsTypeList()) {
assertEquals(good.toString(), good.getInitialBuyPrice(), dm.getCostToBuy(good));
assertEquals(good.toString(), good.getInitialSellPrice(), dm.getPaidForSale(good));
}
}


public void testEuropeMarketPricing(){
Game g = getStandardGame();

Player p = g.getPlayerByNationId("model.nation.dutch");

Specification s = spec();

Europe eu = p.getEurope();

for (GoodsType good : s.getGoodsTypeList()) {
List<AbstractGoods> goods = new ArrayList<AbstractGoods>();
assertEquals(p.getMarket().getSalePrice(good, 1), eu.getOwner().getMarket().getSalePrice(good, 1));
goods.add(new AbstractGoods(good, 1));
int bidPrice = p.getMarket().getBidPrice(good, 1);
int buyCost = p.getMarket().getCostToBuy(good);
int priceGoods = eu.priceGoods(goods);
assertEquals(buyCost, bidPrice);
assertEquals(buyCost, priceGoods);
}
}



/**
* Serialization and deserialization?
Expand Down

0 comments on commit 01abb56

Please sign in to comment.